package com.itheima.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.itheima.common.TokenHolder;
import com.itheima.entity.dto.order.OrderSaveDto;
import com.itheima.entity.dto.order.OrderStatusDto;
import com.itheima.entity.pojo.AddressBook;
import com.itheima.entity.pojo.Order;
import com.itheima.entity.pojo.OrderDetail;
import com.itheima.entity.pojo.ShoppingCart;
import com.itheima.entity.vo.order.*;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.mapper.OrderDetailMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author huangzelnag
 * @create 2022-05-12
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    AddressBookMapper addressBookMapper;
    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;


    /**
     * 分页条件查询订单
     * @param beginTime
     * @param endTime
     * @param number
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public OrderPageVo getOrderPage(Date beginTime, Date endTime, String number, Integer page, Integer pageSize) {
        //1.创建前端返回对象
            OrderPageVo orderPageVo = new OrderPageVo();
            orderPageVo.setCurrent(page);
            orderPageVo.setSize(pageSize);

        //2.查询总条数
        Integer count = orderMapper.selectCountPageCondition(beginTime, endTime, number);
        orderPageVo.setTotal(count);


        //3.查询分页列表
        page=(page-1)*pageSize;

        List<Order> orders = orderMapper.selectPage(beginTime, endTime, number, page, pageSize);
        orderPageVo.setRecords(orders);


        return orderPageVo;
    }

    /**
     * 修改订单状态
     *
     * @param orderStatusDto
     * @return
     */
    @Override
    public Boolean upOrderStatus(OrderStatusDto orderStatusDto) {


        return   orderMapper.updateStatus(orderStatusDto);
    }

    /**
     * 提交订单
     *
     * @param orderSaveDto
     * @return
     */
    @Override
    public Boolean saveOrder(OrderSaveDto orderSaveDto) {
        //1.订单对象
        Order order = new Order();
        BeanUtils.copyProperties(orderSaveDto, order);

//        String addressBookId = orderSaveDto.getAddressBookId();
//        AddressBook addressBook =addressBookMapper.selectid(addressBookId);

//        BeanUtils.copyProperties(addressBook, order);


        String userId = TokenHolder.getCurrentId();

        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectBymoeny(userId);
//        long time1 =System.currentTimeMillis()/1000;
//        System.out.println("1:"+time1);
        Snowflake snowflake = IdUtil.getSnowflake();
        String orderId =snowflake.nextIdStr();

//        long time2 =System.currentTimeMillis()/1000;
//        System.out.println("2:"+(time2-time1));


        BigDecimal sum=new BigDecimal(0);


        //创建订单详情联系表
        for (ShoppingCart shoppingCart : shoppingCarts) {

            long time3 =System.currentTimeMillis()/1000;
            System.out.println("1:"+time3);
            BigDecimal amount = shoppingCart.getAmount();
            Integer number = shoppingCart.getNumber();
            BigDecimal num =new BigDecimal(number);
            sum = sum.add(amount.multiply(num));

            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart, orderDetail);
            orderDetail.setId(snowflake.nextIdStr());

            orderDetail.setOrderId(orderId);

            long time4=System.currentTimeMillis()/1000;
            System.out.println("2:"+(time4-time3));
            orderDetailMapper.insert(orderDetail);
        }
        //创建订单表
        order.setOrderTime(new Date());
        order.setCheckoutTime(new Date());
        order.setAmount(sum);
        order.setPayMethod(1);
        order.setUserId(userId);

        order.setId(orderId);
        order.setNumber(orderId);
        orderMapper.insert(order);

        //1.订单详情表
        shoppingCartMapper.deleteByUserId(userId);

        return true;
    }

    /**
     * 获取用户分页订单
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public OrderUserPageVo getOrderUserPage(Integer page, Integer pageSize) {
        OrderUserPageVo orderUserPageVo = new OrderUserPageVo();
        orderUserPageVo.setPages(page);
        orderUserPageVo.setCurrent(page);

          String userId = TokenHolder.getCurrentId();

          Integer total =    orderMapper.selectUserIdCount(userId);

          Integer pages =total/pageSize;
          orderUserPageVo.setPages(pages);

          orderUserPageVo.setTotal(total);
         List<OrderUserPageLVo> orderUserPageLVos = orderMapper.selectUserIdPage(userId,page,pageSize);
         for (OrderUserPageLVo orderUserPageLVo : orderUserPageLVos) {
             String orderId = orderUserPageLVo.getId();

             List<OrderDetailsVo> orderDetailsVos =  orderDetailMapper.selectByOrderId(orderId);
             orderUserPageLVo.setOrderDetails(orderDetailsVos);
        }


        orderUserPageVo.setRecords(orderUserPageLVos);




        return orderUserPageVo;
    }
}
